//
//  NoticeWarnViewController.m
//  Rail
//
//  Created by hychen on 2018/2/28.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "NoticeWarnViewController.h"
#import "NoticeWarnTableViewCell.h"
#import "NoticeDetailViewController.h"

@interface NoticeWarnViewController ()

@end

@implementation NoticeWarnViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = self.view.backgroundColor;
    
    [self.navigationItem addSimpleTitleView:@"公告通知"];
    
    [self setIsOpenFooter:YES];
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshData {
    self.currentPage = 1;
    BMWeakSelf
    [HttpRequest loadNoticeListWithPage:[NSString stringWithFormat:@"%ld",self.currentPage] success:^(NSArray *object) {
        if ([object isKindOfClass:[NSArray class]]) {
            weakSelf.dataArray = [NSMutableArray arrayWithArray:object];
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_footer resetNoMoreData];
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_header endRefreshing];
        Show_Msg_Error(msg)
    }];
}

- (void)loadMoreData {
    self.currentPage++;
    BMWeakSelf
    [HttpRequest loadNoticeListWithPage:[NSString stringWithFormat:@"%ld",self.currentPage] success:^(NSArray *object) {
        if ([object isKindOfClass:[NSArray class]] && object.count > 0) {
            [weakSelf.dataArray addObjectsFromArray:object];
            [weakSelf.tableView.mj_footer endRefreshing];
            [weakSelf.tableView reloadData];
        }
        else {
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_footer endRefreshing];
        Show_Msg_Error(msg)
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NoticeWarnTableViewCell *cell = [NoticeWarnTableViewCell cellWithTableView:tableView class:[NoticeWarnTableViewCell class]];
    NoticeModel *model = [self.dataArray objectAtIndexSafe:indexPath.row];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NoticeModel *model = [self.dataArray objectAtIndexSafe:indexPath.row];
    if ([model isKindOfClass:[NoticeModel class]]) {
        //跳转
        NoticeDetailViewController *web = [NoticeDetailViewController new];
        web.model = model;
        [self.navigationController pushViewController:web animated:YES];
    }
}

@end
